package com.mlz.mlzmall.ui.activity.act.main;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.personal.AddNewAddress;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.AgainLoginService;
import com.mlz.mlzmall.ui.activity.act.personal.PersonalActivity;
import com.mlz.mlzmall.ui.activity.act.personal.ReceiveAddress;
import com.mlz.mlzmall.ui.model.StringTools;
import com.mlz.mlzmall.ui.utils.TimerUtils;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.mlz.mlzmall.ui.utils.vali.PermissionsActivity;
import com.mlz.mlzmall.ui.utils.vali.PermissionsChecker;

import java.util.Timer;

public class WelComeActivity extends BaseActivity {

    private boolean isFirstIn = false;
    private static final int TIME = 2000;
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private SharedPreferences sp;
    public boolean isNetworkEnable;

    private static final int REQUEST_CODE = 0; // 请求码
    // 所需的全部权限
    protected static final String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.SEND_SMS
    };
    protected PermissionsChecker mPermissionsChecker; // 权限检测器
    ConnectivityManager connectivityManager;
    TelephonyManager telephonyManager;
    public String deviceId;

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    startIntent();//进入登陆
                    break;
                case GO_GUIDE:
                    goGuide();//引导界面
                    break;
            }

        }

        ;
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.welcome_layout);

        mPermissionsChecker=new PermissionsChecker(this);

        inHandler();
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mobNetInfo.isConnected() || wifiNetInfo.isConnected()) {
            isNetworkEnable = true;
        } else {
            isNetworkEnable = false;
        }
        telephonyManager = (TelephonyManager) WelComeActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = telephonyManager.getDeviceId();
        ACache cache = ACache.get(getApplication());
        String sessiongkey = cache.getAsString("sessionKeyLogin");
        String phoneid = cache.getAsString("phoneid");
//        Log.e("phoneid", phoneid + "\t\t" + sessiongkey + "\t\t" + telephonyManager.getDeviceId().toString());
        cache.put("telephony", deviceId);
        if (sessiongkey != null && phoneid != null && isNetworkEnable) {
            Intent intent1 = new Intent(WelComeActivity.this, AgainLoginService.class);
            stopService(intent1);
            Intent intent = new Intent(WelComeActivity.this, AgainLoginService.class);
            intent.putExtra("again", 1);
            startService(intent);
        }

    }
//
    private void inHandler() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                init();//判断是否第一次安装，是就打开引导不页，不是就进入登陆界面
            }
        }, 10 * 3);
    }

    private void init() {
        sp = getSharedPreferences(ConfigInfo.WELCOME, MODE_PRIVATE);
        isFirstIn = sp.getBoolean(ConfigInfo.GUIDE, true);
        SharedUtils.put(this,ConfigInfo.DEVICEID,"android");
        if (!isFirstIn) {
            mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);

        } else {
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);


        }
        if(StringTools.isLoginSession(this)){
            System.out.println("==Timer");
            Timer timer = new Timer("againloginthread");
            TimerUtils timerUtils = new TimerUtils(this);
            timer.schedule(timerUtils, 0, 15* 60 * 1000);
        }

    }

    private void goGuide() {
        //以后要用的别删了这是去找引导页的
        Editor editor = sp.edit();
        editor.putBoolean(ConfigInfo.GUIDE, false);
        editor.commit();
        Intent i = new Intent(WelComeActivity.this, GuideActivity.class);
        WelComeActivity.this.overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        startActivity(i);
        WelComeActivity.this.finish();
    }

    private void startIntent() {
        Intent intent = new Intent(WelComeActivity.this, MainActivity.class);
     //    Intent intent = new Intent(WelComeActivity.this, ReceiveAddress.class);
        startActivity(intent);
        WelComeActivity.this.overridePendingTransition(R.anim.push_back_down_in, R.anim.push_back_down_in);
        WelComeActivity.this.finish();

    }

    @Override
    protected void onResume() {
        super.onResume();

        // 缺少权限时, 进入权限配置页面
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            startPermissionsActivity();
        }
    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            finish();
        }
    }
}